package com.emep.changzhi.analyse.service.systeminfo;

import com.emep.changzhi.analyse.entity.systeminfo.SysMenuInfo;
import com.emep.changzhi.analyse.entity.systeminfo.SysRoleInfo;
import com.emep.changzhi.analyse.repository.systeminfo.SysMenuInfoRepository;
import com.emep.changzhi.analyse.repository.systeminfo.SysRoleInfoRepository;
import com.emep.changzhi.analyse.utils.MapUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* @author Wangcc
* @Description: 角色信息业务层
* @date 2018/2/24 14:32
* @version V1.0
*/
@Slf4j
@Service
public class SysRoleInfoService {

    @Resource
    private SysRoleInfoRepository sysRoleInfoRepository;

    @Resource
    private SysMenuInfoRepository sysMenuInfoRepository;
    
    /**
     *
     * 不含查询条件的分页获取数据
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Page<SysRoleInfo> findAll(int pageNo, int pageSize){
        try {
            return sysRoleInfoRepository.findAll(new PageRequest(pageNo-1, pageSize, new Sort(Sort.Direction.ASC, "id")));
        } catch (Exception e) {
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
            return null;
        }
    }

    /**
     *
     * 新增角色
     *
     * @param payload
     * @return
     * @throws Exception
     */
    public SysRoleInfo add(Map<String, Object> payload) {
        SysRoleInfo sysRoleInfo = null;
        Set<SysMenuInfo> sysMenuInfos = new HashSet<>();
        try {
            SysRoleInfo sysRoleInfo1 = (SysRoleInfo) MapUtils.convertMap(SysRoleInfo.class,payload);
            //遍历菜单id
            if(payload.containsKey("menuId")){
                String[] menuIds = payload.get("menuId").toString().split(",");
                for(String menuId:menuIds){
                    SysMenuInfo sysMenuInfo = sysMenuInfoRepository.findSysRoleInfoById(menuId);
                    if(sysMenuInfo != null){
                        sysMenuInfos.add(sysMenuInfo);
                    }
                }
                sysRoleInfo1.setSysMenuInfos(sysMenuInfos);
            }
            sysRoleInfo = sysRoleInfoRepository.save(sysRoleInfo1);
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return sysRoleInfo;
    }


    /**
     *
     * 根据id查询角色信息
     *
     * @param id
     * @return
     */
    public SysRoleInfo findId(String id){
        SysRoleInfo sysUserInfo = null;
        try {
            sysUserInfo = sysRoleInfoRepository.findSysRoleInfoById(id);
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return sysUserInfo;
    }

    /**
     *
     * 根据id 删除角色信息
     *
     * @param id
     */
    public Boolean delete(String id) {
        Boolean result = false;
        try {
            Integer result1 = sysRoleInfoRepository.deleteById(id);
            if(result1 == 1){
                result = true;
            }
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return result;
    }

    /**
     *查询所有角色信息
     *
     * @return
     */
    public List<SysRoleInfo> list() {
        List<SysRoleInfo> sysRoleInfo = null;
        try {
            sysRoleInfo =  sysRoleInfoRepository.findAll();
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return sysRoleInfo;
    }

}
